package com.deyuanyun.pic.pipe.support.utils;

import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.ApplicationConfigUtil;
import com.deyuanyun.pic.common.util.DateUtil;
import com.deyuanyun.pic.domain.base.UnifiedEncoding;
import com.deyuanyun.pic.domain.pipe.PipeLine;
import com.deyuanyun.pic.mapper.pipe.PipeLineMapper;
import com.deyuanyun.pic.pipe.domain.exceldomain.CrossingOverInfoVO;
import com.deyuanyun.pic.support.biz.CodeUtil;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by xu on 2016/3/14.
 * 统一编码规则
 */
public class UniformCodeUtils {
    static Logger logger=Logger.getLogger(UniformCodeUtils.class);
    static Map<String,String> codeTableName=new HashMap<>();
    static {
        codeTableName.put("穿跨越","pic_pipe_pipecross");
        codeTableName.put("收发球装置","pic_pipe_piggingstructure");
        codeTableName.put("地面标识","PIC_PIPE_IDENTIFICATION");
        codeTableName.put("定标点","PIC_PIPE_TRACK_POINT");
        
        codeTableName.put("三通","PIC_PIPE_TEE");
        codeTableName.put("阀门","PIC_PIPE_VALVE");
        codeTableName.put("热煨弯头","PIC_PIPE_ELBOW");
        codeTableName.put("冷弯弯头","PIC_PIPE_CELBOW");
    }

    public static String getCodePart1(String lineId, String typeName){
        if ("收发球装置".equals(typeName)){
            typeName="收发球筒";
        }
        UnifiedEncoding encoding = CodeUtil.findEncodByName(typeName);//3
        if (encoding==null){return null;}
        String encodingName=encoding.getAbridge();
        String company= ApplicationConfigUtil.getConfigByName("companyCode");//1
            PipeLine pipeLine = PipeDictUtils.getLineById(lineId);
        String lineAbridge = pipeLine.getAbridge();//2

        String codePart1=company+lineAbridge+encodingName;
        return codePart1;

    }


    /**
     * 将记录按照管道和绝对距离进行分类排序
     * @param list
     * @param lineIdName  管段字段名
     * @param sortName  排序字段
     *                  codeName   统一编码字段名
     *                  typeName 数据类型 穿跨越 三通...
     * @param <T>
     * @return
     */
    public static<T> List<T> sortedByLine(List<T> list,
                                          String lineIdName,
                                          String sortName,
                                          String codeName,
                                          String typeName) throws Exception{
        List<T> newList=new ArrayList<>();
        Multimap multimap = HashMultimap.create();
        for (T t:list){
            String lineId = BeanUtils.getProperty(t,lineIdName);
            if (StringUtils.isBlank(lineId)){
                continue;
            }
            multimap.put(lineId,t);
        }

        Set<String> keys = multimap.keySet();
        for (String k:keys){
            String tableName=codeTableName.get(typeName);
            int lineCount=0;
            if (StringUtils.isNotBlank(tableName)){
                PipeLineMapper pipeLineMapper= SpringAppliction.getBean(PipeLineMapper.class);
                Map mapm=new HashMap();
                mapm.put("tableName",tableName);
                mapm.put("feildName","Pipe_line_id");
                mapm.put("lineId",k);
                lineCount=pipeLineMapper.countUniformCode(mapm);
            }


            List<T> ll = new ArrayList<>(multimap.get(k));
            //Collections.sort(ll,newCom(sortName));//因为该字段在导入时还没有值，所以不能排序
            for (int i=0;i<ll.size();i++){
                T o=ll.get(i);
                String c=BeanUtils.getProperty(o,codeName);
                if (StringUtils.isNotBlank(c)){
                    newList.add(o);
                    continue;
                }
                String p1=getCodePart1(k,typeName);
                //如果是定标点
                if ("PIC_PIPE_TRACK_POINT".equals(tableName)){
                    String recordtime=BeanUtils.getProperty(o,"RECORDTIME");
                    if (StringUtils.isBlank(recordtime)&&recordtime.length()<6){
                        recordtime= DateUtil.DateToStringYMDHMS(new Date());
                    }
                    recordtime=StringUtils.replaceEach(recordtime,new String[]{"/","-",":"," "},new String[]{"","","",""});
                    recordtime=StringUtils.substring(recordtime,0,6);
                    BeanUtils.setProperty(o,codeName,p1+recordtime+StringUtils.leftPad((i+1+lineCount+""),6,"0"));
                }else {
                    BeanUtils.setProperty(o,codeName,p1+StringUtils.leftPad((i+1+lineCount+""),4,"0"));
                }
                newList.add(o);
            }
        }
        return newList;
    }

    private static Comparator newCom(final String sortName){
        return new Comparator(){
            @Override
            public int compare(Object o1, Object o2) {
                BigDecimal o1v=null;
                BigDecimal o2v=null;
                try {
                    Object oo1 = BeanUtils.getProperty(o1,sortName);
                    Object oo2 = BeanUtils.getProperty(o2,sortName);
                    o1v=new BigDecimal((String)oo1);
                    o2v=new BigDecimal((String)oo2);
                } catch (Exception e) {
                    logger.error(e);
                    return 0;
                }
                return o1v.compareTo(o2v);
            }
        };
    }


    public static void main(String[] args) throws Exception{
        List<CrossingOverInfoVO> crossingOverInfoVOList = new ArrayList<>();

        CrossingOverInfoVO c1=new CrossingOverInfoVO();
        c1.setPipeLineId("wert6");
        c1.setCrossingNumber("1");
        CrossingOverInfoVO c2=new CrossingOverInfoVO();
        c2.setPipeLineId("wert6");
        c2.setCrossingNumber("4");
        CrossingOverInfoVO c3=new CrossingOverInfoVO();
        c3.setPipeLineId("wert6");
        c3.setCrossingNumber("3");
        CrossingOverInfoVO c4=new CrossingOverInfoVO();
        c4.setPipeLineId("wert3");
        c4.setCrossingNumber("2");

        crossingOverInfoVOList.add(c1);
        crossingOverInfoVOList.add(c2);
        crossingOverInfoVOList.add(c3);
        crossingOverInfoVOList.add(c4);




        List d= sortedByLine(crossingOverInfoVOList,"pipeLineId","crossingNumber","code","xx");
        String xxx="";
    }


}
